package com.lizard.schedule.widget.socialshare;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lizard.schedule.R;
import com.lizard.schedule.utils.DeviceUtil;

import java.util.ArrayList;
import java.util.List;

class ShareGridAdapter extends BaseAdapter {

    private List<SocialBean> mSocials = new ArrayList<SocialBean>();

    private LayoutInflater mInflater;

    public ShareGridAdapter(Context context, List<SocialBean> socials){
        mInflater = LayoutInflater.from(context);
        if(socials != null){
            mSocials.addAll(socials);
        }
    }

    @Override
    public int getCount() {
        return mSocials.size();
    }

    @Override
    public Object getItem(int i) {
        return mSocials.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        Holder holder;
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.social_item, null);
            holder = new Holder();
            ListView.LayoutParams params = new ListView.LayoutParams(
                    ListView.LayoutParams.MATCH_PARENT, DeviceUtil.getScreenWidth() / 4);
            convertView.setLayoutParams(params);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(holder);
        } else{
            holder = (Holder) convertView.getTag();
        }

        SocialBean socialBean = (SocialBean)getItem(i);

        if(socialBean.getIconResId() <= 0){
            holder.icon.setImageDrawable(null);
        } else{
            holder.icon.setImageResource(socialBean.getIconResId());
        }
        holder.title.setText(socialBean.getTitle());
        convertView.setOnClickListener(socialBean.getOnClick());

        return convertView;
    }

    class Holder{
        ImageView icon;
        TextView title;
    }
}
